import requests
import unittest
from parameterized import parameterized
import json


def build_data():
    test_data = []
    file = "./data/login.json"
    with open(file, encoding="utf-8") as f:
        json_data = json.load(f)
        for case_data in json_data:
            username = case_data.get("username")
            password = case_data.get("password")
            verify_code = case_data.get("verify_code")
            status_code = case_data.get("status_code")
            status = case_data.get("status")
            msg = case_data.get("msg")
            test_data.append((username, password, verify_code, status_code, status, msg))
    print("test_data=".format(username, password, verify_code, status_code, status, msg))
    return test_data

class TPShopLogin2(unittest.TestCase):

    def setUp(self):
        self.session = requests.Session()
        self.url_verify = '''http://10.10.1.1/index.php?m=Home&c=User&a=verify'''
        self.url_login = '''http://10.10.1.1/index.php?m=Home&c=User&a=do_login'''

    def tearDown(self):
        self.session.close()

    @parameterized.expand(build_data())
    def test01_login(self, username, password, verify_code, status_code, status, msg):
        response = self.session.get(url=self.url_verify)
        # 结果断言
        self.assertEqual(200, response.status_code)  # 断言相应状态码200
        self.assertIn("image", response.headers.get("Content-Type"))

        # 登录测试
        login_data = {
            'username': username,
            'password': password,
            'verify_code': verify_code
        }
        response = self.session.post(url=self.url_login, data=login_data)
        print(response.json())
        self.assertEqual(status_code, response.status_code)
        self.assertEqual(status, response.json().get('status'))
        self.assertIn(msg, response.json().get("msg"))




